Page({
    data: {
        name: '',
        nameErr: false,
        buttonLoading: false,
        agreement: true, //同意协议
    },
    submit() {
        const { name, agreement } = this.data;
        if (name === '') return this.setData({ nameErr: true });

        if (!agreement)
            return wx.showToast({
                title: '请阅读并同意《用户服务协议》和《隐私政策》',
                icon: 'none',
            });
        this.setData({ buttonLoading: true });

        let reqData = { name };
        this.request(
            {
                url: '/api/user/updateName',
                method: 'POST',
                data: reqData,
            },
            () => {
                this.getUserInfo();
                wx.showToast({
                    title: '认证成功',
                    icon: 'success',
                    duration: 800,
                    mask: true,
                });
                this.delay(() => {
                    wx.navigateBack();
                }, 800);
            },
            () => {
                this.setData({ buttonLoading: false });
            }
        );
    },
    clearErr() {
        this.setData({
            nameErr: false,
        });
    },
    //协议
    onAgreeChange({ detail }) {
        this.setData({ agreement: detail });
    },
});
